package com.haierp.controller.authority;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.model.authority.OrganizationHai;
import com.haierp.model.authority.UserHai;
import com.haierp.service.authority.IOrganizationServiceHai;
import com.haierp.service.authority.IUserServiceHai;
import com.haierp.util.JsonPageResult;
import com.haierp.util.JsonResult;
import com.haierp.vo.authority.OrganizationQueryVO;

/**
 * 
 * Title: OrganizationControllerHai.java
 * Description: 
 *
 * @author jc
 * Apr 20, 2017
 *
 */
@Service("organizationControllerHai")
@Controller
@RequestMapping("/organizationHai")
public class OrganizationControllerHai {

	@Autowired
	private IOrganizationServiceHai iOrganizationServiceHai;
	
	@Autowired
	private IUserServiceHai iUserServiceHai;

	@RequestMapping("/add")
	@ResponseBody
	public Object add(OrganizationHai organization) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null == organization.getId()) {
			
			if (StringUtils.isBlank(organization.getName())) {
				return result.buildMsg("部门名称不能为空").buildIsSuccess(false);
			}
			
			if (StringUtils.isBlank(organization.getCode())) {
				return result.buildMsg("编号不能为空").buildIsSuccess(false);
			}
			
	        if (iOrganizationServiceHai.checkOrganizationNameIsExist(organization.getName(), null)) {
	            result.buildMsg("部门名称已存在!").buildIsSuccess(false);
	        } else {
	        	organization.setCreateTime(new Date());
	        	iOrganizationServiceHai.insert(organization);
		        result.buildIsSuccess(true);
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Object update(OrganizationHai organization) {
		JsonResult<String> result = new JsonResult<>();
		
		if (null != organization.getId()) {
			if (StringUtils.isBlank(organization.getName())) {
				return result.buildMsg("部门名称不能为空").buildIsSuccess(false);
			}
			
			if (StringUtils.isBlank(organization.getCode())) {
				return result.buildMsg("编号不能为空").buildIsSuccess(false);
			}
			
	        if (iOrganizationServiceHai.checkOrganizationNameIsExist(organization.getName(),organization.getId())) {
	            result.buildMsg("部门名称已存在!").buildIsSuccess(false);
	        } else {
	        	organization.setCreateTime(new Date());
	        	iOrganizationServiceHai.updateById(organization);
		        result.buildIsSuccess(true);
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(Long id) {
		JsonResult<String> result = new JsonResult<>();

		if (null != id) {
			UserHai user = new UserHai();
			user.setOrganizationId(id.intValue());
			EntityWrapper<UserHai> entityWrapper = new EntityWrapper<UserHai>(user);
			Integer count = iUserServiceHai.selectCount(entityWrapper);
			if ((null != count) && (count > 0)) {
				result.buildMsg("此部门有关联的用户，无法删除").buildIsSuccess(false);
			} else {
				iOrganizationServiceHai.deleteById(id);
				result.buildIsSuccess(true);
			}
		} else {
			result.buildMsg("错误数据").buildIsSuccess(false);
		}
		
		return result;
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public Object query(Long id) {
		JsonResult<OrganizationHai> result = new JsonResult<>();
		
		return result.buildData(iOrganizationServiceHai.selectById(id)).buildIsSuccess(true);
	}
	
	@RequestMapping("/queryList")
	@ResponseBody
	public Object queryList(OrganizationQueryVO organizationQueryVO) {
		JsonPageResult<List<OrganizationHai>> result = iOrganizationServiceHai.queryOrganizationList(organizationQueryVO);

		return result.buildIsSuccess(true);
	}
}
